package com.itheima.pinda.controller.agency;


import com.itheima.pinda.DTO.entity.dto.PdFleetDTO;
import com.itheima.pinda.DTO.entity.pojo.agency.PdFleet;
import com.itheima.pinda.DTO.entity.vo.Results;
import com.itheima.pinda.DTO.entity.vo.StatusCode;
import com.itheima.pinda.feign.service.agency.PdFleetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 车队表 前端控制器
 * </p>
 *
 * @author xy
 * @since 2021-11-12
 */
@RestController
@RequestMapping("/base/pdFleet")
public class PdFleetController {

    @Autowired
    private PdFleetService fleetService;


    /**
     * 根据id获取车队详情
     *
     * @param id 车队id
     * @return 车队信息
     */
    @GetMapping("/{id}")
    public PdFleetDTO fineById(@PathVariable(name = "id") String id) {
        PdFleet pdFleet = fleetService.getById(id);
        PdFleetDTO dto = new PdFleetDTO();
        BeanUtils.copyProperties(pdFleet, dto);
        return dto;
    }




    /**
     * 删除fleet
     * @param id
     * @return
     */
    @GetMapping("/disable/{fid}")
    public Results disable(@PathVariable(name = "fid") String fid){
        boolean upd = fleetService.deleteUpd(fid);
        if (upd){
            return new Results(true, StatusCode.OK,"删除成功",null);

        }
        return new Results(false,StatusCode.ERROR,"删除失败",null);
    }

    /**
     * 添加fleet
     * @param name
     * @param manager
     * @return Results
     */
    @PostMapping("/save")
    public Results saveFleet(@RequestParam(value = "name") String name,
                             @RequestParam(value = "manager") String manager,
                             @RequestParam(value = "agencyId") String agencyId,
                             @RequestParam(value = "phone") String phone){
        System.out.println("name："+name);
        System.out.println("manager："+manager);
        boolean upd = fleetService.saveFleet(name,manager,agencyId,phone);
        if (upd){
            return new Results(true, StatusCode.OK,"添加成功",null);

        }
        return new Results(false,StatusCode.ERROR,"添加失败",null);
    }




    /**
     * 获取车队列表
     *
     */
    @PostMapping("")
    public Results<List<PdFleetDTO>> findAdvance(@RequestParam(value = "name",required = false) String name ,
                                             @RequestParam(value = "fleetNum",required = false) String fleetNum,
                                             @RequestParam(value = "manager",required = false) String manager) {
        List<PdFleetDTO> fleet = fleetService.findAllFleet(name,fleetNum,manager);
        return new Results<>(true,StatusCode.OK,"asf",fleet);
    }


}
